﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;

namespace Fu3DEngine
{
    [Serializable]
  public   class TerrainData
    {
          #region 属性封装

      

       

        vtkRenderer window;

        public vtkRenderer Window
        {
            get { return window; }
            set { window = value; }
        }
        double x, y, z;

        public double X
        {
            get { return x; }
            set { x = value; }
        }

        public double Y
        {
            get { return y; }
            set { y = value; }
        }

        public double Z
        {
            get { return z; }
            set { z = value; }
        }
        vtkImageData imgData;

        public vtkImageData ImgData
        {
            get { return imgData; }
            set { imgData = value; }
        }
        float[] spatialR;//每个格子边长的实际意义

        public float[] SpatialR
        {
            get { return spatialR; }
            set { spatialR = value; }
        }
        double[] bounds;

        public double[] Bounds
        {
            get { return bounds; }
            set { bounds = value; }
        }
        double scalefactor;

        public double Scalefactor
        {
            get { return scalefactor; }
            set { scalefactor = value; }
        }
        int rows;

        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }
        int cols;

        public int Cols
        {
            get { return cols; }
            set { cols = value; }
        }
        float minHeight;

        public float MinHeight
        {
            get { return minHeight; }
            set { minHeight = value; }
        }
        float maxHeight;

        public float MaxHeight
        {
            get { return maxHeight; }
            set { maxHeight = value; }
        }

        #endregion
        public TerrainData(vtkRenderer window, double x, double y, double z, vtkImageData imgData, float[] spatialR, double[] bounds, double scalefactor, int rows, int cols, float minHeight, float maxHeight)
        {
            this.window = window;
            this.x = x;//原点坐标，左下角坐标
            this.y = y;
            this.z = z;
            this.imgData = imgData;
            this.spatialR = spatialR;
            this.bounds = bounds;
            this.scalefactor = scalefactor;
            this.rows = rows;
            this.cols = cols;
            this.minHeight = minHeight;
            this.maxHeight = maxHeight;
           

        }

        public double GetBaseHeight(double x, double y)
        {
            double myx = x - this.x;
            double myy = y - this.y;

            myx = myx / (bounds[1] - bounds[0]) * cols;
            myy = myy / (bounds[3] - bounds[2]) * rows;

            //边界检查
            if (myx > cols)
            {
                myx = cols;
            }

            if (myx < 0)
            {
                myx = 0;
            }
            if (myy > rows)
            {
                myy = rows;
            }

            if (myy < 0)
            {
                myy = 0;
            }

            double baseheight = imgData.GetScalarComponentAsDouble((int)myx, (int)myy, 0, 0);
            double height;

            //计算实际的高度
            height = baseheight / maxHeight * bounds[5];

            return height;



        }
        public double[] GetTerrainPoint(double x, double y)
        {
            double myx = x - this.x;
            double myy = y - this.y;



            myx = myx / (bounds[1] - bounds[0]) * cols;
            myy = myy / (bounds[3] - bounds[2]) * rows;

            //边界检查
            if (myx > cols-1)
            {
                myx = cols-1;
            }

            if (myx < 0)
            {
                myx = 0;
            }
            if (myy > rows-1)
            {
                myy = rows-1;
            }

            if (myy < 0)
            {
                myy = 0;
            }

            double baseheight = imgData.GetScalarComponentAsDouble((int)myx, (int)myy, 0, 0);
            double[] mypoint = new double[3];

            //计算空间位置坐标
            mypoint[0] = (bounds[1] - bounds[0]) * myx / rows;
            mypoint[1] = (bounds[3] - bounds[2]) * myy / cols;

            //计算实际的高度
            mypoint[2] = baseheight / maxHeight * bounds[5] ;

            return mypoint;




        }

        public double GetHeightFromMeter(double meter)
        {

            return meter / maxHeight * bounds[5];

        }


    }
}
